package se.webbzon.boltzmann.editor.list;

import java.util.Collection;

import javax.swing.ComboBoxModel;

public class NullableComboBoxModel<T> extends NullableListModel<T> implements ComboBoxModel<T> {

	// Auto generated serial version UID
	private static final long serialVersionUID = -5683403090462039654L;
	
	// The selected item of this model
	private T selection;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Created a new empty nullable combo box model. **/
	public NullableComboBoxModel() {
		this(null);
	}
	
	/** Created a new nullable combo box model with a given content. **/
	public NullableComboBoxModel(Collection<T> collection) {
		super(collection);
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override public Object getSelectedItem() {
		return selection;
	}

	@SuppressWarnings("unchecked")
	@Override public void setSelectedItem(Object anItem) {
		if (anItem == null || !containsElement((T) anItem))
			selection = null;
		else
			selection = (T) anItem;
		fireContentsChanged(this,0,getSize()-1);
	}

}
